from django.db import models

ENGINE_CHOICES = [
    ('mysql', 'MySQL'),
    ('postgresql', 'PostgreSQL'),
    ('sqlite', 'SQLite'),
    ('oracle', 'Oracle'),
    # Add more engines as needed
]

class DataSource(models.Model):
    name = models.CharField(max_length=100, unique=True)
    engine = models.CharField(max_length=100, choices=ENGINE_CHOICES,default='mysql')
    user = models.CharField(max_length=100, blank=True, null=True)
    password = models.CharField(max_length=100, blank=True, null=True)
    host = models.CharField(max_length=100, blank=True, null=True)
    port = models.IntegerField(blank=True, null=True)
    description = models.TextField(blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    
    def __str__(self):
        return self.name

class Table(models.Model):
    name = models.CharField(max_length=100)
    data_source = models.ForeignKey(DataSource, on_delete=models.CASCADE, related_name='tables')
    description = models.TextField(blank=True, null=True)
    is_show = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        unique_together = ('name', 'data_source')
    
    def __str__(self):
        return f"{self.name} ({self.data_source.name})"  

class Field(models.Model):
    FIELD_TYPE_CHOICES = [
        ('string', '字符串'),
        ('integer', '整数'),
        ('float', '浮点数'),
        ('boolean', '布尔值'),
        ('date', '日期'),
        ('datetime', '日期时间'),
        ('text', '文本'),
    ]
    
    name = models.CharField(max_length=100)
    table = models.ForeignKey(Table, on_delete=models.CASCADE, related_name='fields',null=True, blank=True)
    field_type = models.CharField(max_length=20, choices=FIELD_TYPE_CHOICES)
    description = models.TextField(blank=True, null=True)
    is_required = models.BooleanField(default=False)
    is_show = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        unique_together = ('name', 'table')
    
    def __str__(self):
        return f"{self.name} ({self.table.name})"    